package com.jeady.jTools.feedback;

import android.os.Handler;
import android.os.Message;
import android.util.JsonReader;
import android.util.Log;

import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class SCommThread extends Thread {
    private String TAG = "InquireSth Class";
    private String url_str = "http://zhnc.cn/s.php?fun=";
    private StringBuffer data = new StringBuffer();
    private Handler handler;
    public StringBuffer get_data() {
        return data;
    }
    public SCommThread(int witch, @Nullable Handler handler, @Nullable String opt1, @Nullable String opt2) {
        this.handler = handler;
        String arg = "";
        switch (witch) {
            case 0x0a:
                arg = "0";
                break;
            case 0x0b:
                arg = "1";
                break;
            case 0x100:
                if(opt1 == null)
                    arg = "100&where=null";
                else
                    arg = "100&where=" + opt1;
                break;
            case 0x101:
                arg = String.format(Locale.CHINA, "%d&date=%s&msg=%s", 101, opt1, opt2);
                break;
        }
        url_str = url_str + arg;
    }

    @Override
    public void run() {
//                        String url_str = "http://www.baidu.com";
        Message msg = new Message();
        try {
            URL url = new URL(url_str);
//            Log.e("SComm", "run: request url=" + url.toString());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(1000);
            int code = connection.getResponseCode();
            msg.what = 0;
            msg.arg1 = code;
            if (code == 200) {
                Log.d(TAG, "connect server");
                InputStream inputStream = connection.getInputStream();
                BufferedReader buffered = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                while ((line = buffered.readLine()) != null) {
                    data.append(line);
                }
                System.out.println(data.toString() + "\n\n");
                msg.obj = data;
            } else if (code == 440) {
                Log.e(TAG, "run: 找不到网页， 返回代码440");
            }
        } catch (IOException e) {
            e.printStackTrace();
            msg.arg1 = -1;
        }
        if(this.handler != null){
            handler.sendMessage(msg);
        }
    }
}
